// pages/component/toast/toast.js
const TOAST_CONFIG = [{
        type: "success",
        icon: "success_no_circle",
        className: ""
    },
    {
        type: "cancel",
        icon: "cancel",
        className: ""
    },
    {
        type: "hint",
        icon: "",
        className: ""
    }
]


Component({
    /**
     * 组件的属性列表
     */
    properties: {
        opt: {
            type: Object,
            value: {}
        }
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    /**
     * 组件的方法列表  
     */
    methods: {
        show(opt) {
            console.log(opt)
            //匹配icon类型，显示对应icon
            for (const key in TOAST_CONFIG) {
                if (TOAST_CONFIG[key].type == opt.type) {
                    opt.type = TOAST_CONFIG[key].icon
                }
            }
            this.setData({
                opt: opt
            })
            setTimeout(() => {
                this.hide()
            }, this.data.opt.duration)
        },
        hide() {
            this.data.opt.show = !this.data.opt.show
            this.setData({
                opt: this.data.opt
            })
            this.data.opt.success();  //执行回调
        },
    }
})